<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="模版消息管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px" label-position="left">

            <el-form-item label="消息类型" prop="messageType">
                <el-select v-model="formData.messageType" filterable clearable placeholder="请选择">
                    <el-option
                        v-for="item in Dict.enums.messageType"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="模板名称" prop="name">
                <el-input v-model="formData.name"/>
            </el-form-item>

            <el-form-item label="模板code" prop="code">
                <el-input v-model="formData.code"/>
            </el-form-item>

            <el-form-item label="模板数据" prop="fields" label-position="top">
                <el-input v-model="formData.fields" type="textarea" :rows="8"/>
            </el-form-item>

            <el-form-item label="模板消息内容" prop="content">
                <el-input v-model="formData.content"  type="textarea" :rows="2"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {TemplateApi} from "@/api/message/TemplateApi";
import {Common} from '@/utils/common'

export default {
    name: 'MessageTemplateUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                messageType: '',
                name: '',
                code: '',
                fields: '',
                content: ''
            },
            rules: {
                messageType: [
                    {required: true, message: '请选择消息类型', trigger: 'change'}
                ],
                name: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                code: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                fields: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                content: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ]
            },
            dialogVisible: false
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        async showUpdate(data) {
            const that = this

            this.dialogVisible = true

            if (this.$refs['formRef'] !== undefined) {
                this.$refs['formRef'].resetFields()
            }

            this.$nextTick(() => {
                if (data.id > 0) {
                    that.formData = Common.assign(this.formData, data)
                    that.formData.fields = JSON.stringify(that.formData.fields,null,4)
                    that.formData.messageType = that.formData.messageType.toString()
                } else {
                    this.formData.id = ''
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    const submitData = Object.assign({}, that.formData)
                    submitData.fields = JSON.parse(submitData.fields)
                    if (submitData.id > 0) {
                        TemplateApi.edit(submitData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        TemplateApi.add(submitData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>

<style>
</style>
